# 6: TRACE
# 5: VERBOSE
# 4: DEBUG
# 3: INFO
# 2: WARN
# 1: ERROR

log ?= 6
cpu ?=
dbg ?= 1
out ?= build
toolchain ?=


CC := $(toolchain)g++

AR := $(toolchain)ar

CC_FLAG := -std=c++14 $(cpu) -DLOG_LEVEL=$(log)

AR_FLAG := rcs

LD_FLAG := -shared

FLAG_DEBUG := -O0 -g

FLAG_RELEASE := -O2 -w -DNDEBUG=1

ifeq ($(dbg), 0)
CC_FLAG += $(FLAG_RELEASE)
else
CC_FLAG += $(FLAG_DEBUG)
endif


jlib_static := $(out)/out/libjlib.a
jlib_shared := $(out)/out/libjlib.so

include_dirs :=
include_dirs += -I/usr/include
include_dirs += -I/usr/local/include
include_dirs += -I/usr/java/default/include
include_dirs += -I/usr/java/default/include/linux

library_dirs :=
library_dirs += -L/usr/lib
library_dirs += -L/usr/local/lib

dependencies :=
dependencies += -lcrypto
dependencies += -lssl
dependencies += -lpthread

headers :=
headers += base/macro.h
headers += base/logger.h
headers += base/ns.h
headers += base/types.h
headers += base/obj.h
headers += base/obj_pointer.h
headers += base/obj_cache.h
headers += base/buffer.h
headers += base/byte_buffer.h
headers += base/auto_close.h
headers += base/bits.h
headers += base/list.h
headers += base/gmath.h
headers += base/random.h
headers += arch/x86.h
headers += arch/avx.h
headers += arch/a64.h
headers += arch/asm.h
headers += number/integer.h
headers += number/decimal.h
headers += string/options.h
headers += cipher/sum.h
headers += cipher/crc.h
headers += cipher/hex.h
headers += cipher/b64.h
headers += cipher/md5.h
headers += cipher/sha.h
headers += cipher/aes.h
headers += cipher/rsa.h
headers += container/bit_set.h
headers += container/block_queue.h
headers += container/ordered_map.h
headers += parallel/lock.h
headers += parallel/executor.h
headers += parallel/scheduler.h
headers += misc/invoke_tracker.h
headers += misc/steady_timer.h
headers += vmath/vec2i.h
headers += vmath/scalar.h
headers += vmath/scalar_cxx.h
headers += vmath/scalar_avx.h
headers += vmath/scalar_a64.h
headers += vmath/vec2.h
headers += vmath/vec2_cxx.h
headers += vmath/vec2_avx.h
headers += vmath/vec2_a64.h
headers += vmath/vec3.h
headers += vmath/vec3_cxx.h
headers += vmath/vec3_avx.h
headers += vmath/vec3_a64.h
headers += vmath/vec4.h
headers += vmath/vec4_cxx.h
headers += vmath/vec4_avx.h
headers += vmath/vec4_a64.h
headers += vmath/mat4.h
headers += vmath/mat4_cxx.h
headers += vmath/mat4_avx.h
headers += vmath/mat4_a64.h
headers += jlib.h

sources :=
sources += base/obj.cpp
sources += base/obj_cache.cpp
sources += base/bit_buffer.cpp
sources += base/random.cpp
sources += number/integer.cpp
sources += number/decimal.cpp
sources += string/options.cpp
sources += cipher/hex.cpp
sources += cipher/b64.cpp
sources += cipher/aes.cpp
sources += cipher/rsa.cpp
sources += misc/invoke_tracker.cpp
sources += graphics/node/node.cpp
sources += graphics/node/root_node.cpp
sources += graphics/action/mapper.cpp
sources += graphics/action/action.cpp
sources += graphics/action/immediate_action.cpp
sources += graphics/action/continued_action.cpp


.PHONY : test shared static asm

shared : $(headers) $(sources) $(jlib_shared)
shared : CC_FLAG += -fPIC

test : static $(out)/out/cipher.test $(out)/out/number.test $(out)/out/string.test $(out)/out/vmath.test

static : $(headers) $(sources) $(jlib_static)

asm : $(headers) $(sources:%.cpp=$(out)/asm/jlib/%.s)

install : shared static
	cp -rf $(jlib_shared) /usr/local/lib/
	cp -rf $(jlib_static) /usr/local/lib/


$(jlib_shared) : $(sources:%.cpp=$(out)/obj/jlib/%.o)
	@mkdir -p $(@D)
	$(CC) $(LD_FLAG) -o $@ $^ $(library_dirs) $(dependencies)

$(jlib_static) : $(sources:%.cpp=$(out)/obj/jlib/%.o)
	@mkdir -p $(@D)
	$(AR) rcs $@ $^


$(out)/out/cipher.test : ../test/cipher/main.cpp
	@mkdir -p $(@D)
	$(CC) $(CC_FLAG) -o $@ $^ $(library_dirs) -L$(out)/out -Wl,-Bstatic -ljlib -Wl,-Bdynamic $(dependencies)

$(out)/out/number.test : ../test/number/main.cpp
	@mkdir -p $(@D)
	$(CC) $(CC_FLAG) -o $@ $^ $(library_dirs) -L$(out)/out -Wl,-Bstatic -ljlib -Wl,-Bdynamic $(dependencies)

$(out)/out/string.test : string/test.cpp
	@mkdir -p $(@D)
	$(CC) $(CC_FLAG) -o $@ $^ $(library_dirs) -L$(out)/out -Wl,-Bstatic -ljlib -Wl,-Bdynamic $(dependencies)

$(out)/out/vmath.test : vmath/test.cpp vmath/test1.cpp
	@mkdir -p $(@D)
	$(CC) $(CC_FLAG) -o $@ $^ $(library_dirs) -L$(out)/out -Wl,-Bstatic -ljlib -Wl,-Bdynamic $(dependencies)

$(out)/obj/jlib/%.o : %.cpp
	@mkdir -p $(@D)
	$(CC) -c $(CC_FLAG) -o $@ $^ $(include_dirs)

$(out)/asm/jlib/%.s : %.cpp
	@mkdir -p $(@D)
	$(CC) -S $(CC_FLAG) -o $@ $^ $(include_dirs)


